package com.dymall.common.interceptors;

import cn.hutool.core.util.StrUtil;
import com.dymall.common.utils.UserContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截动作在网关已做，该拦截器不需要做拦截，对请求一律放行
public class UserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userInfo = request.getHeader("user-info");
        if(StrUtil.isNotBlank(userInfo)){
            //非空非null，则存入ThreadLocal
            UserContext.setUser(Long.valueOf(userInfo));
        }
        return true;
    }//springMVC的拦截器要生效需要经过配置 MvcConfig

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //执行完后删除ThreadLocal中的用户
        UserContext.removeUser();
    }
}
